/**
 * Created by liubin on 2017/3/5.
 */


var WhmjTableRoomEndLayer = GameCCSBase.extend({
    Panel_players: null,
    mHorno : null,
    Panel_honors : null,
    mJuEndedEventwhmj : null,
    mViewSeatUids : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/WhmjTableRoomEnd.json");
        mo.gameHelper.fixIosText(this.ccsNode);
        this.Panel_players = [];
        this.mViewSeatUids = [];

        mo.seekWidgetByName(this.ccsNode, "Button_close").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                // mo.whmjTabel.quitWhmjGame();
                if (mo.sceneHelper.currentLayerName == "login"){
                    //在登录界面就会带登录
                    mo.sceneHelper.intoLoginLayer();
                }else{
                    mo.sceneHelper.runSceneHall();
                }
                mo.sceneHelper.closeEndInfoLayer();
            }
        }, this);

        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.gameHelper.developerVersion == "3.0"){
                    mo.gameHelper.onWxShareFriend("新蜂麻将-牌局结算-卡五星", "我完成了一个新蜂麻将的牌局，这个是结算截图！");
                }else if (mo.gameHelper.developerVersion == "2.0"){
                    mo.gameHelper.onWxShareFriend("新蜂棋牌-牌局结算-卡五星", "我完成了一个新蜂棋牌的牌局，这个是结算截图！");
                }
            }
        }, this);

        this.loadUI();

        this.setVisible(false);

        if (mo.gameHelper.developerVersion == "2.0"){
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_gameName"), "武汉麻将");
        }else if (mo.gameHelper.developerVersion == "3.0"){
            mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_gameName"), "新蜂麻将");
        }

    },

    onEnter : function(){
        this._super();

        mo.eventHelper.add("PushRYScore", this, function(PushRYScore){
            this.setHonorInfo(PushRYScore);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadUI: function () {
        for (var i = 0; i < 3; i++) {
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player" + i);
            this.Panel_players.push(Panel_player);
        }

        //点赞
        this.Panel_honors = [];
        for (var i = 0; i < 4; i++) {
            var Panel_honor = mo.seekWidgetByName(this.ccsNode, "Panel_honor" + i);
            this.Panel_honors.push(Panel_honor);

            Panel_honor.setVisible(false);

            var Button_like = mo.seekWidgetByName(Panel_honor, "Button_like");
            var Button_fuck = mo.seekWidgetByName(Panel_honor, "Button_fuck");

            Button_like.setTag(i);
            Button_fuck.setTag(i);

            Button_like.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();
                    this.setHonorIsLick(index, true);

                    mo.seekWidgetByName(this.Panel_honors[index], "Button_like").setEnabled(false);
                    mo.seekWidgetByName(this.Panel_honors[index], "Button_fuck").setTouchEnabled(false);
                }
            }, this);

            Button_fuck.addTouchEventListener(function (sender, type) {
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    var index = sender.getTag();
                    this.setHonorIsLick(index, false);

                    mo.seekWidgetByName(this.Panel_honors[index], "Button_like").setTouchEnabled(false);
                    mo.seekWidgetByName(this.Panel_honors[index], "Button_fuck").setEnabled(false);
                }
            }, this);
        }

    },

    setRoomEndInfo : function(juEndedEventwhmj){
        this.mJuEndedEventwhmj = juEndedEventwhmj;

        //房间规则
        var rule = "";
        var ruleJson = JSON.parse(juEndedEventwhmj.CfgJson);
        rule += ("卡五星*" + ruleJson.whmjrate);
        rule += ("  \n杠上炮*" + ruleJson.gangpaorate);
        rule += ("\n碰碰胡*" + ruleJson.pengpengrate);
        rule += ("  \n最大番数*" + ruleJson.alltop);
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_rule"), rule);

        //房间号
        var tableIdStr = "房间号:" + juEndedEventwhmj.TableId;
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_room"), tableIdStr);

        //时间
        var date = new Date();
        var time = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " ";
        time += (date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_time"), time);

        //游戏玩法
        var Image_wanfa = mo.seekWidgetByName(this.ccsNode, "Image_wanfa");
        var wanfaImageFile = [
            "res/ccs/whmj/table/end/wanfa_xg.png",
            "res/ccs/whmj/table/end/wanfa_xy.png",
            "res/ccs/whmj/table/end/wanfa_sy.png",
            "res/ccs/whmj/table/end/wanfa_sz.png"
        ];
        mo.gameHelper.loadTextureImage(Image_wanfa, wanfaImageFile[ruleJson.ruleid - 1]);

        //游戏局数
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_jushu"), juEndedEventwhmj.curpan + "/" + ruleJson.maxpan);

        //大赢家
        var bigWinIndex = 0;
        var tempSocore = 0;

        //自己的logicseat
        var myLogicSeat = 0;
        for (var i = 0; i < 4; i++){
            if (juEndedEventwhmj.Ju[i].Uid == mo.gameUserInfo.uid){
                myLogicSeat = i;
                break;
            }
        }

        for (var i = 0; i < 4; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            var logicSeat = mo.whmjTableStatus.getLogicForView(i, myLogicSeat);
            var juInfo = juEndedEventwhmj.Ju[logicSeat];

            //nick
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_nick"), mo.gameHelper.getUserNickFilter(juInfo.Nick));

            //uid
            this.mViewSeatUids.push(juInfo.Uid);
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_uid"), juInfo.Uid);

            //head
            mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(player, "Image_head"), juInfo.FaceUrl);

            //info
            var infoStr = "";
            infoStr += ("自摸        " + juInfo.Zm);
            infoStr += ("\n接炮        " + juInfo.Pao);
            infoStr += ("\n点炮        " + juInfo.Paoed);
            infoStr += ("\n暗杠        " + juInfo.Gang2);
            infoStr += ("\n明杠        " + juInfo.Gang1);
            infoStr += ("\n................");
            infoStr += ("\n总成绩    " + juInfo.Score);
            mo.gameHelper.setTextString(mo.seekWidgetByName(player, "Text_info"), infoStr);

            //socore
            if (juInfo.Score > tempSocore){
                tempSocore = juInfo.Score;
                bigWinIndex = i;
            }
        }

        //大赢家
        for (var i = 0; i < 4; i++){
            var player = mo.seekWidgetByName(this.ccsNode, "Image_player" + i);
            var Image_bigWinner = mo.seekWidgetByName(player, "Image_bigWinner");
            Image_bigWinner.setVisible(i == bigWinIndex);
        }
    },

    setHonorInfo : function(PushRYScore){
        this.mHorno = {};
        this.mHorno.Uid = PushRYScore.Uid;
        this.mHorno.key = PushRYScore.Key;
        this.mHorno.Score = [];
        for (var i = 0; i < PushRYScore.Uid.length; i++){
            this.mHorno.Score.push(0);
        }

        if (this.mViewSeatUids.length == 3){
            for (var i = 0; i < PushRYScore.Uid.length; i++){
                var index = mo.gameHelper.findValueForArray(this.mViewSeatUids, PushRYScore.Uid[i]);
                if (index != null){
                    this.Panel_honors[index].setVisible(true);
                }
            }
        }
    },

    setHonorIsLick : function(index, isLick){
        var uid = this.mViewSeatUids[index];
        var index = mo.gameHelper.findValueForArray(this.mHorno.Uid, uid);
        if (index != null){
            this.mHorno.Score[index] = isLick ? 1 : 2;
        }
    },

    sendHonor : function(){
        if (this.mHorno != null && this.mHorno.Uid.length != 0){
            mo.gameMsgManager.sendRYScore(this.mHorno.Uid, this.mHorno.key, this.mHorno.Score);
        }
    }
});
